<!-- 院系提交资格认定 -->
<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="导师编号">
              <a-input v-model="queryParam.dsbh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="招生单位">
                <a-select allowClear v-model="queryParam.zsdw" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="指导专业">
                <a-select allowClear v-model="queryParam.zdzy" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="导师状态">
                <a-select allowClear v-model="queryParam.shzt">
                  <a-select-option v-for="i in TYPE0320" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="是否在岗">
                <a-select allowClear v-model="queryParam.dssfzg">
                  <a-select-option v-for="i in TYPE0321" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="招生年度">
                <a-select v-model="queryParam.zsnd" @change="zsndChange">
                  <a-select-option v-for="i in TYPE0387" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="导师类别">
                <a-select mode="multiple" allowClear v-model="queryParam.dslbm">
                  <a-select-option v-for="i in TYPE0360" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="审核状态">
                <a-select   v-model="queryParam.dstjzt">
                  <a-select-option :value="1">已提交</a-select-option>

                  <a-select-option :value="2">已审核</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="24" :sm="48">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <a-button type="primary" @click="exportExcel">导出资格认定</a-button>
              <a-button type="primary" @click="checkZgrd" :disabled="checkDisabled" style="margin-left: 8px">审核资格认定</a-button>
              <a-button type="danger" @click="backZgrd" :disabled="backDisabled" style="margin-left: 8px">退回资格认定</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{ x: 2500 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
          <!-- :class="{'color-red': record | isOverage}"  -->
        <span 
          slot="csrq" 
          :class="{'color-red': isOverage(record)}" 
          slot-scope="text, record">{{ text && text.slice(0, 7)}}</span>
        <span slot="psffrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a>
      </s-table>
    </a-card>
  </div>
</template>

<script>
  import {STable} from '~'
  import {mapState} from 'vuex'
  import recruitUnit from './components/recruitUnit'
  import breakRule from './components/breakRule'
  import contract from './components/contract'
  import searchMixin from '@/utils/searchRemain'
  import moment from 'moment'

  export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      checkDisabled: true,
      backDisabled: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '导师编号',
          dataIndex: 'dsbh',
          scopedSlots: { customRender: 'dsbh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '出生年月',
          dataIndex: 'csrq',
          scopedSlots: { customRender: 'csrq' }
        },
        {
          title: '招生年度',
          dataIndex: 'zsnd'
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '指导专业',
          dataIndex: 'zdzymc',
          width:180
        },
        {
          title: '导师类别',
          dataIndex: 'dslbmc'
        },
        {
          title: '研究方向',
          dataIndex: 'yjfxmc',
          width: '220px'
        },
        {
          title: '招生单位',
          dataIndex: 'zsdwmc'
        },
        {
          title: '招生层次',
          dataIndex: 'zsccmc'
        },
        {
          title: '导师职称',
          dataIndex: 'zcmc'
        },
        {
          title: '导师状态',
          dataIndex: 'shztmc'
        },
        {
          title: '聘书发放日期',
          dataIndex: 'psffrq',
          scopedSlots: { customRender: 'psffrq' }
        },
        {
          title: '是否在岗',
          dataIndex: 'dssfzgmc'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        this.$refs.table && this.$refs.table.clearSelected()
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        // this.queryParam.dstjzt=1;
        params={
          ...params
        }
        return this.$api.teacher.teacherListByZgrd(params).then(res => {
          console.log(res.data)
          this.checkZgrdStatus()
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yx:[]
      },
      teacherInfo: {},
      initDone: false
    }
  },
  components: {STable, recruitUnit, breakRule, contract},
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0360: state => state.app['TYPE0360'], // 导师类别
      TYPE0320: state => state.app['TYPE0320'], // 导师状态
      TYPE0321: state => state.app['TYPE0321'], // 是否在岗
      TYPE0387: state => state.app['TYPE0387'] // 招生学年
    })
  },
  // beforeRouteEnter (to, from, next) {
  //   next(vm => {
  //     let pageParams = vm.$store.state.app.pageParams || {}
  //     if ((from.name === '导师详细信息') && pageParams.path === vm.$route.path) {
  //       vm.queryParam = pageParams.params
  //       vm.parameter = pageParams.pagination
  //       return vm.initSelectMenu(1)
  //     }
  //     vm.initSelectMenu()
  //   })
  // },
  // beforeRouteLeave (to, from, next) {
  //   if (to.name === '导师详细信息') {
  //     this.$store.dispatch('app/setPageParams', {
  //       path: this.$route.path,
  //       params: this.queryParam,
  //       pagination: this.parameter
  //     })
  //   }
  //   next()
  // },
  created () {
    // this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0360')
    this.$store.dispatch('app/setDictionary', 'TYPE0320')
    this.$store.dispatch('app/setDictionary', 'TYPE0321')
    this.$store.dispatch('app/setDictionary', 'TYPE0387')
    // this.$store.dispatch('app/sysInfo').then(res => {
    //   this.$set(this.queryParam, 'zsnd', res.masterAdmissionYear)
    //   this.$set(this.queryParam, 'dstjzt', 1)
    //   this.initDone = true
    // })
  },
  methods: {
    initSelectMenu (noFirst = 0) {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if (!noFirst) {
          this.$set(this.queryParam, 'zsnd', res[1].masterAdmissionYear)
          this.$set(this.queryParam, 'dstjzt', 1)
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    isOverage(value) {
      if(value.csrq) {
        if (value.dslbm.includes('1') && moment(value.csrq) < moment(this.systemInfo.tutorMasterAdmissionAgeLine)) {
          return true
        }
        if (value.dslbm.includes('2') && moment(value.csrq) < moment(this.systemInfo.tutorDoctoralAdmissionAgeLine)) {
          return true
        }
      }
      return false
    },
    yxChange (code) {
      this.checkZgrdStatus()
      this.$set(this.queryParam, 'zdzy', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    zsndChange(){
      this.checkZgrdStatus()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    checkZgrd () {
      if (!this.queryParam.zsdw || !this.queryParam.zsnd) {
        this.$message.warning('请选择指导院系和招生年度')
        return
      }
      this.$confirm({
        title: '确认框',
        content: '确定通过资格认定吗?',
        centered: true,
        onOk: () => {
          this.$api.teacher.checkZgrd({zsnd: this.queryParam.zsnd, yxm: this.queryParam.zsdw}).then(() => {
            this.$message.success('审核成功')
            this.$refs.table.refresh(false)
          })
        },
        onCancel: () => {
        }
      })
    },
    backZgrd () {
      if (!this.queryParam.zsdw || !this.queryParam.zsnd) {
        this.$message.warning('请选择指导院系和招生年度')
        return
      }
      this.$confirm({
        title: '确认框',
        content: '确定退回资格认定吗?',
        centered: true,
        onOk: () => {
          this.$api.teacher.backZgrd({zsnd: this.queryParam.zsnd, yxm: this.queryParam.zsdw}).then(() => {
            this.$refs.table.refresh(false)
            this.$message.success('退回成功')
          })
        },
        onCancel: () => {
        }
      })
    },
    checkZgrdStatus(){
      if(!this.queryParam.zsdw || !this.queryParam.zsnd){
        return;
      }
      this.$api.teacher.getCheckStatus({zsnd: this.queryParam.zsnd, yxm: this.queryParam.zsdw}).then((res) => {
        if (!res.data || res.data.dstjzt === '0') {
          this.checkDisabled = false
          this.backDisabled = false
        } else if(res.data.dstjzt === '1'){
          this.checkDisabled = false
          this.backDisabled = false
        } else if(res.data.dstjzt === '2'){
          this.checkDisabled = true
          this.backDisabled = false
        }
      })
    },
    direct (data) {
      this.$router.push(`/teacher/info/${data.dsbh}`)
    },
    exportExcel(){
      this.$export('teacher/dsxx/exportZgrd',this.queryParam)
    }
  }
}
</script>

<style scoped>
.color-red{
  color:  red;
}
</style>
